package demo09;

/**
 * @author JYH
 * @version 1.0
 * @date 2022/11/1 17:12
 */
public class TicketLockTest {
    public static void main(String[] args) {
        demo03.Ticket ticket = new demo03.Ticket();

        Thread thread1 = new Thread(ticket,"窗口A");
        Thread thread2 = new Thread(ticket,"窗口B");
        Thread thread3 = new Thread(ticket,"窗口C");
        Thread thread4 = new Thread(ticket,"窗口D");

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();

    }
}
class Ticket implements Runnable{
    private Object lock=new Object();
    private int tickets=100;
    @Override
    public void run() {
        while (true){
            synchronized (lock) {
                if (tickets > 0) {
                    System.out.println(Thread.currentThread().getName() + "卖了一张票,剩余" + --tickets + "张");
                } else {
                    break;
                }
            }
        }
    }
}